<template>
  <div id="app">
    <router-view class="main" />
    <van-tabbar active-color="pink" :fixed="false" route v-show="isShowTabBar">
      <van-tabbar-item icon="home-o" :to="{ name: 'Home' }"
        >首页</van-tabbar-item
      >
      <van-tabbar-item icon="shop-o" :to="{ name: 'List' }"
        >分类</van-tabbar-item
      >
      <van-tabbar-item
        icon="shopping-cart-o"
        :badge="cartNum > 0 ? cartNum : ''"
        :to="{ name: 'Car' }"
        >购物车</van-tabbar-item
      >
      <van-tabbar-item icon="user-o" :to="{ name: 'User' }"
        >我的</van-tabbar-item
      >
    </van-tabbar>
  </div>
</template>
<script>
import { loadCartDataAPI } from "./services/Cart";
import { getToken } from "./utils/tools";
export default {
  data() {
    return {
      isShowTabBar: true,
      cart: [],
    };
  },
  watch: {
    $route(v) {
      if (v.meta.hideTabBar) {
        this.isShowTabBar = false;
      } else {
        this.isShowTabBar = true;
      }
    },
  },
  created() {
    if (getToken()) {
      this.cartNumber();
    }
    //监听
    this.$eventBus.$on("cartnum", this.cartNumber);
  },

  methods: {
    async cartNumber() {
      // 获取购物数据
      const res = await loadCartDataAPI();
      // console.log(res);
      this.cart = res.data;
      // console.log(res.data.length);
    },
    // this.cartNumber()
  },
  computed: {
    cartNum: {
      get() {
        return this.cart.length;
      },
    },
  },
};
</script>
<style>
html,
body {
  width: 100%;
  height: 100%;
}
#app {
  height: 100%;
  display: flex;
  flex-direction: column;
  background-color: #f4f4f4;
}
.main {
  flex: 1;
  overflow: auto;
}
</style>
